<?php

namespace App\Http\Controllers\admin\doctor;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\User;
use App\Models\User_info;
use App\Models\Doctor;
use Intervention\Image\ImageManagerStatic as Image;
class DoctorController extends Controller
{
    //药师管理列表
    public function index()
    {
        $doctor = new Doctor;
        $list = $doctor->doctorGet();
        return view('admin.doctor.doctor',['list'=>$list]);

    }

    public function create()
    {
       return view('admin.doctor.doctorAdd');
    }

    public function store(Request $request)
    {
        $doctor = new Doctor();
        $data = $request->except('_token','file','select','password');
        $data['password'] = md5($request->input('password'));

        foreach ($data as $v){
            if(empty($v)){
                return back()->with('success','验证失败请重新填写,请按提示正确填写');
            }
        }

        $user = new User();

        $user->username = time();
        $user->password = 2;
        $user->class = 2;

        $uinsert = $user->save();

        $userid = $user->id;

        $userinfo = new User_info();

        $userinfo->user_id = $userid;

        $userinfo->save();

        $data['user_id'] = $user->id;

        $data['credimg'] = $doctor->photo($request);

        //dd($data);

        $insert = $doctor->create($data);

        $bool = $insert->save();

        if($bool){
            return redirect('/admin/doctor');
        }else{
            return null;
        }

    }

    public function edit($id)
    {
        $list = Doctor::find($id);

        return view('admin.doctor.doctorEdit',['list'=>$list]);
    }

    public function update($id,Request $request)
    {
        $doctor = new Doctor();
        $data = $request->except('_token','_method','file','select');

        $data['credimg'] = $doctor->photo($request);

        $bool = $doctor->where('id',$id)->update($data);

        if($bool){
            return redirect('/admin/doctor');
        }else{
            return null;
        }

    }

    public function status(Request $request)
    {
        $id = $request->input('id');

        $data = Doctor::find($id);

        $data->status = $data->status==3?2:3;

        $bool = $data->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }
    }


    public function editphoto(Request $request)
    {
        $id = $request->input('id');

        $data = Doctor::find($id);

        $data->photo = $this->file($request);
        $img = Store::find($id)->logo;
        if(!empty($img) && file_exists('./upload/doctor/'.$img)){
            unlink('./upload/doctor/'.$img);
        }


        $bool = $data->save();

        if($bool){
            return back();
        }
    }

    public function credInfo(Request $request)
    {
        $id = $request->input('id');

        $data = Doctor::find($id,['credname','crednum','credimg']);

        return $data;
    }

    public function userInfo(Request $request)
    {
        $id = $request->input('id');

        $data = Doctor::find($id,['name','idcard','phone','age','summary','address','sex']);

        return $data;
    }

    public function file(Request $request)
    {
        //dd($request->hasFile('files'));
        if ($request->hasFile('photo')) {
            if ($request->file('photo')->isValid()) {
                $file = $request->file('photo');
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
                $img = Image::make($file);
                $img->resize(400, null, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $img->save('./upload/doctor/'.$picname);

                return $picname;

            }
        }
    }

}
